<template>
  <!-- 新闻详情页底部封装 -->
  <div class="comment_s">
    <div class="addcomment" v-show="!isFocus">
      <input type="text" placeholder="写跟帖" @focus="handlerFocus" />
      <span
        class="comment"
        @click="$router.push({ path: '/comment/' + article.id })"
      >
        <i class="iconfont iconpinglun-"></i>
        <em>{{ article.comment_length }}</em>
      </span>
      <i
        class="iconfont iconshoucang"
        :class="{ active: article.has_star }"
        @click="collection"
      ></i>
      <i class="iconfont iconfenxiang"></i>
    </div>
    <div class="inputcomment" v-show="isFocus">
      <textarea
        ref="commtext"
        rows="5"
        v-model="content"
        @blur="handlerBlur"
      ></textarea>
      <div>
        <span @click="send">发 送</span>
        <span @click="hand">取 消</span>
      </div>
    </div>
  </div>
</template>

<script>
import { post_star } from "@/apis/post.js";
import { get_comment } from "@/apis/user.js";
export default {
  props: {
    article: {
      type: Object,
      required: true,
    },
    contents: {
      type: Object,
      default: null,
    },
  },
  data() {
    return {
      isFocus: false,
      content: "",
    };
  },
  watch: {
    contents() {
      if (this.contents) {
        this.isFocus = true;
        setTimeout(() => {
          this.$refs.commtext.focus();
        }, 10);
      }
    },
  },
  methods: {
    hand() {
      this.isFocus = false;
      this.content = "";
      this.$emit("resr");
    },
    handlerBlur() {
      setTimeout(() => {
        this.isFocus = false;
        this.content = "";
        this.$emit("resr");
      }, 20);
    },
    handlerFocus() {
      this.isFocus = true;
      setTimeout(() => {
        this.$refs.commtext.focus();
      }, 10);
    },
    async send() {
      if (this.content.trim().length == 0) {
        this.$toast("请输入评论内容");
        return;
      } else {
        let data = {
          content: this.content,
        };
        if (this.contents) {
          data.parent_id = this.contents.id;
        }
        let res = await get_comment(this.article.id, data);
        this.content = "";
        this.isFocus = false;
        this.$emit("refresh");
      }
    },

    async collection() {
      let res = await post_star(this.article.id);
      if (res.data.message == "收藏成功") {
        this.$toast.success(res.data.message);
      } else {
        this.$toast.success(res.data.message);
      }
      this.article.has_star = !this.article.has_star;
    },
  },
};
</script>

<style lang="less" scoped>
.comment_s {
  width: 100%;
  position: fixed;
  left: 0;
  bottom: 0;
}
.inputcomment {
  padding: 10px;
  box-sizing: border-box;
  width: 100%;
  display: flex;
  background-color: #fff;
  align-items: flex-end;
  textarea {
    flex: 3;
    background-color: #eee;
    border: none;
    border-radius: 10px;
    padding: 10px;
  }
  div {
    padding: 20px;
  }
  span {
    display: block;
    flex: 1;
    height: 24px;
    line-height: 24px;
    padding: 0 10px;
    background-color: #f00;
    color: #fff;
    text-align: center;
    border-radius: 6px;
    font-size: 13px;
  }
}
.addcomment {
  width: 100%;
  background-color: #fff;
  box-sizing: border-box;
  padding: 10px;
  margin-top: 20px;
  display: flex;
  text-align: center;
  position: absolute;
  bottom: 0;
  left: 0;
  > input {
    flex: 4;
    height: 30px;
    line-height: 30px;
    border-radius: 15px;
    border: none;
    background-color: #eee;
    padding-left: 20px;
    font-size: 14px;
  }
  .active {
    color: #f00;
    font-weight: 700;
  }
  i {
    font-size: 20px;
  }
  > span {
    flex: 1;
    position: relative;
    > em {
      position: absolute;
      right: 0;
      top: -5px;
      font-size: 10px;
      background-color: #f00;
      color: #fff;
      border-radius: 5px;
      padding: 3px 5px;
    }
  }
  > i {
    flex: 1;
  }
}
</style>